//
//  AccountOperation.m
//  MUBATE
//
//  Created by sunbin on 2017/2/20.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "AccountOperation.h"
#import "EditAccount.h"

#import "CorporateCenter.h"

@interface AccountOperation ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *enterAccountFile;
@property (weak, nonatomic) IBOutlet UITextField *enterScodeFile;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UIButton *resentBtn;

@end

@implementation AccountOperation
{
    NSString *code;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [NHUtils pushAndPop:@"EnterPwd" range:NSMakeRange(2, 1) currentCtl:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];


    [self initView];
}

- (void)initView{
    self.row = 3;
    
    NSString *enterAccountType ,*title ,*headTitle;
    switch (self.modifyMake) {
        case Binding_phone:{
        title = LocalizedString(@"Binding mobile phone number");}
            break;
        case Binding_emaill:{
        title = LocalizedString(@"Binding mailbox");}
            break;
        case Change_phoneNum:{
        title = LocalizedString(@"Change a phone number");        }
            break;
        case Change_emaillNum:{
        title = LocalizedString(@"Change the email");
        }
            break;
        default:
            break;
    }
    self.title = title;
    
    headTitle = self.bingAccountType==editPhone?@"填写完手机号后,点击发送验证码,验证后即可绑定与修改":@"填写完邮箱地址后,点击发送验证码,验证后即可绑定与修改";
    self.headTitle = headTitle;
    self.isShowHead = YES;

    enterAccountType = self.bingAccountType==editPhone?LocalizedString(@"phoneNum"):LocalizedString(@"emailAdress");
    
    [_enterAccountFile leftViewModeWithConstrainedToWidth:100.f text:enterAccountType fon:App_textLeftViewFon setFont:App_textFon leftTextColor:[UIColor appLeftTextColor] bodyTextColor:[UIColor aPPBodyTextColor] PlaceholderColor:[UIColor appPlaceholderColor] setPlaceholder:nil isLaunch:NO];
    
    [_enterScodeFile leftViewModeWithConstrainedToWidth:100.f text:LocalizedString(@"code :") fon:App_textLeftViewFon setFont:App_textFon leftTextColor:[UIColor appLeftTextColor] bodyTextColor:[UIColor aPPBodyTextColor] PlaceholderColor:[UIColor appPlaceholderColor] setPlaceholder:nil isLaunch:NO];
    
    [NHUtils setButton:_confirmBtn isLaunch:NO setTitle:LocalizedString(@"confirm") setLayerCornerRadius:0 setFont:15.f];
    _confirmBtn.enabled = NO;
   
}


// 监听输入
- (IBAction)editingChang:(UITextField *)sender{
    [_enterAccountFile bs_hideError];

    self.confirmBtn.enabled = ![NHUtils isBlankString:_enterScodeFile.text] && ![NHUtils isBlankString:_enterScodeFile.text];
}


// 确认键
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    //重新发送 验证
    
    //确认键盘提交
    [self.view endEditing:YES];

    return YES;
}

// 点击按钮 重新发送 确认按钮
- (IBAction)btnEen:(UIButton *)sender {
    NSInteger index = sender.tag-20;//0发送 1提交
    WEAKSELF;
    
    //判断手机号 邮箱是否 正确
    BOOL isTrue =  weakSelf.bingAccountType==editPhone?[_enterAccountFile.text isMobileNumberClassification]:[_enterAccountFile.text isEmailAddress];
    
    if (isTrue) {
        [weakSelf sendRequest:index];
    }else{
    //显示错误 邮箱 手机号错误
        [_enterAccountFile bs_setupErrorMessageViewWithMessage:weakSelf.bingAccountType==editPhone?LocalizedString(@"phone number is wrong"):LocalizedString(@"Wrong email input")];
        [_enterAccountFile bs_showError];
    }
}


- (void)sendRequest:(NSUInteger)index{
    // 模拟加载
    WEAKSELF;
    [weakSelf.navigationController.view showLoading:@""];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.navigationController.view hideHUD];
        
        [EditAccount editty:weakSelf.bingAccountType modifyMake:weakSelf.modifyMake codeOperate:index editAccount:weakSelf.enterAccountFile.text scode:_enterScodeFile.text success:^(NSDictionary *successResults, NSString *successMsg) {
            
            switch (index) {
                case 0:
                    // 发送验证吗
                    [weakSelf.resentBtn startTime:59 title:@"重新发送" waitTittle:@"s" respond:^(BOOL isPlayComple) {
                        if (isPlayComple) {
                            
                        }
                    }];
                    break;
                case 1:{
                    // 提交验证码 看登录状态
                    BOOL isPhoneLog = [NHUtils isPhoneLog];
                    if (weakSelf.bingAccountType == editPhone) {
                        if (isPhoneLog) {
                           // 退出 发通知
                            [[NSNotificationCenter defaultCenter] postNotificationName:LOGOUT object:nil];

                        }else{
                           // 返回个人中心
                            [weakSelf backToCorporateCenter];

                        }
                    }else{
                        if (!isPhoneLog) {
                            // 退出 发通知
                            [[NSNotificationCenter defaultCenter] postNotificationName:LOGOUT object:nil];

                        }else{
                            // 返回个人中心
                            [weakSelf backToCorporateCenter];
                        }
                    }
                }
                    
                    
                    
                    
                    break;
                    
                    
                    
                default:
                    break;
            }
            
            
            
        } failure:^(NSDictionary *failedResults, responseState resMake, NSString *failedMsg) {
            //失败
            [weakSelf.view showError:failedMsg];
        }];        
    });


    
    
}


- (void)backToCorporateCenter{
    WEAKSELF;
    CorporateCenter *center;
    for (UIViewController *ctl in weakSelf.navigationController.viewControllers) {
        if ([ctl isKindOfClass:[RootViewController class]]) {
            center = (CorporateCenter*)ctl;
        }}
    
    if (self.modifyMake == Change_phoneNum) {
        self.userInfor.store_phone = _enterAccountFile.text;
    }else{
        self.userInfor.store_email = _enterAccountFile.text;
    }
    kAppDelegate.userInfo = self.userInfor;
    
    
    if (center) {
        [weakSelf.navigationController popToViewController:center animated:YES];
    }
    

}

#pragma make -UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    [_enterAccountFile bs_hideError];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}








@end
